/** @odoo-module **/
import {Toast} from './components/Toast.js';

const {
    markup,
    Component,
    onWillStart,
    useEnv,
    useSubEnv,
    useChildSubEnv,
    useRef,
    useState,
    mount,
    xml,
    whenReady,
    onMounted,
    onError,
    loadFile,
    onWillUpdateProps,
    useEffect,
    useComponent,
    EventBus
} = owl;

const weekStr = '日一二三四五六';

class Root extends Component {

    setup() {

        console.log(this.props);

        this.bus = new EventBus();

        useSubEnv({
            bus: this.bus
        });

        useChildSubEnv({// 只会在组件的子组件中挂载公共数据及方法
            closeToast: () => {
                this.state.toastOpen = false;
                this.state.toastData = {};
            },
            openToast:(data)=>{
                this.state.toastOpen = true;
                this.state.toastData = {...data};
            },
        });

        let date = new Date();

        let today_str = `${date.getFullYear()}年${date.getMonth()+1}月${date.getDate()} 星期${weekStr[date.getDay()]}`;

        this.index = {};
        this.menuList = [];
        this.state = useState({
            loading:false,
            isMenuMode: true,
            toastOpen: false,
            toastData: {},//提示消息
            menuList:[],
            log_out_menu:{},
            today_str:today_str,
            designVersion:1, //旧版本0  新版本1
            form:{
                name:'',
                password:''
            },
            errorTips:{
                name:'',
                password:''
            }
        });

        onWillStart(async () => await this.LoadData());
        onWillUpdateProps(async next_props => await this.LoadData());
        // onMounted(()=>{});

        this.handleSubmit = _.debounce(this.submit,300);
    }
    async LoadTestData() {

        let data = await fetch('./json/data.json')
            .then((response) => response.json());
        return data

    }

    async LoadData() {
        this.state.loading = true;
        console.log(`加载数据 `); // 开辟加载父类数据结构逻辑!!!
        // let load_func = this.props.LoadData ? this.props.LoadData : this.LoadTestData.bind(this);
        //
        // let data = await load_func();
        // let {
        //     menu,
        //     org,
        //     user
        // } = data;


        this.state.loading = false;
    }
    submit(){
        let flag = this.checkForm();
        let name = this.state.form.name;
        let password = this.state.form.password;
        name = name.trim();
        password = password.trim();

        if(flag){
            console.log('登录名',name);
            console.log('密码',password)
        };
    }
    checkForm(){
        let name = this.state.form.name;
        let password = this.state.form.password;
        name = name.trim();
        password = password.trim();
        if(!name){
            this.state.errorTips.name = '请输入登录名';
        }else{
            this.state.errorTips.name = '';
        }
        if(!password){
            this.state.errorTips.password = '请输入密码';
        }else{
            this.state.errorTips.password = '';
        }
        if(this.state.errorTips.password || this.state.errorTips.name){
            return false;
        }else{
            return true;
        }

    }
}


Root.template = 'Gmm_login_Root';


Root.props = {
    LoadData: {type: Function, optional: true},
    GotoMenuId: {type: Function, optional: true},
};


Root.components = {
    Toast
};


export {
    Root
}
